const Mock = require('mockjs')

const data = Mock.mock({
  'list|25': [{
    id: '@id',
    username: '@cname',
    'role|+1': ['admin', 'editor'],
    create_time: '@date',
    update_time: '@date'
  }]
})

module.exports = [
  {
    url: '/vue-admin-template/user/list',
    type: 'get',
    response: config => {
      const { username, id, pageNum = 10, pageSize = 1 } = config.query
      const mockList = data.list.filter(item => {
        if (id && item.id !== id) return false
        if (username && item.username !== username) return false
        return true
      })
      const dataList = mockList.filter(
        (item, index) =>
          index < pageNum * pageSize && index >= pageNum * (pageSize - 1)
      )
      return {
        code: 20000,
        data: {
          total: data.list.length,
          items: dataList
        }
      }
    }
  },
  {
    url: '/vue-admin-template/user/del',
    type: 'delete',
    response: config => {
      const { id } = config.query
      data.list = data.list.filter(item => item.id !== id)
      return {
        code: 20000,
        data: {
          msg: '删除成功'
        }
      }
    }
  },
  {
    url: '/vue-admin-template/user/add',
    type: 'post',
    response: config => {
      const { username, role } = config.body
      data.list.unshift({
        id: '@id',
        username,
        role,
        create_time: '@date',
        update_time: '@date'
      })
      return {
        code: 20000,
        data: {
          msg: '添加成功'
        }
      }
    }
  }
]
